﻿using Mimas.Core.Current;
using Mimas.Framework.Shared.Data.DataTransferObjects;
using System;
using System.Linq;

namespace Mimas.Framework.Server.DomainObjects.Mapping
{
    public static class LookupListItemMapping
    {
        public static LookupListItem Map(LookupListItemDTO data)
        {
            LookupListItem newItem = new LookupListItem();

            newItem.Explanation = data.Explanation;
            newItem.Id = data.Id;
            newItem.Name = Culture.ToTitleCase(data.Name);
            newItem.LookupListId = data.LookupListId;
            newItem.SecondaryValue = data.SecondaryValue;

            if (data.Id == 0)
            {
                newItem.CreatedBy = UserInfo.CurrentUser.UserId;
                newItem.CreatedOn = DateTime.Now;
            }
            else
            {
                newItem.ModifiedBy = UserInfo.CurrentUser.UserId;
                newItem.ModifiedOn = DateTime.Now;
            }

            return newItem;
        }

        public static LookupListItemDTO Map(LookupListItem data)
        {
            LookupListItemDTO newItem = new LookupListItemDTO();

            newItem.Explanation = data.Explanation;
            newItem.Id = data.Id;
            newItem.Name = Culture.ToTitleCase(data.Name);
            newItem.LookupListId = data.LookupListId;
            newItem.SecondaryValue = data.SecondaryValue;

            return newItem;
        }

        public static LookupListItem[] Map(LookupListItemDTO[] data)
        {
            if (data == null || !data.Any()) return null;
            return data.Where(x => x != null).Select(x => Map(x)).ToArray();
        }

        public static LookupListItemDTO[] Map(LookupListItem[] data)
        {
            if (data == null || !data.Any()) return null;
            return data.Where(x => x != null).Select(x => Map(x)).ToArray();
        }
    }
}
